package com.geostar.constructionland.analyse.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.geostar.constructionland.common.entity.aly.AlyCode;
import com.geostar.constructionland.common.entity.logs.LogSpt;
import com.geostar.constructionland.common.entity.logs.mongolog.LogMongoPage;
import com.geostar.constructionland.common.entity.logs.mongolog.LogMongoSpt;
import com.geostar.constructionland.common.entity.response.ExtJsAjaxResponse;
import com.geostar.constructionland.common.service.logs.LogSptService;
import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * @author chengan
 * @description momgo修改以及删除查看
 */


@RestController
@Api(value = "Mongo日志接口")
@RequestMapping("/logMongo")
@Slf4j
@Validated
public class LogMongoController {

    @Autowired
    MongoTemplate mongoTemplate;

    @Resource
    private LogSptService logSptService;


    @ApiOperation(value = "新建mongo日志文档")
    @PostMapping("/add")
    @ResponseBody
    public ExtJsAjaxResponse insertMongoLog(@RequestBody LogMongoSpt logMongoSpt) {
        mongoTemplate.insert(logMongoSpt);
        ExtJsAjaxResponse extJsAjaxResponse = ExtJsAjaxResponse.createBySuccessMessage("新建Mongo文档成功");
        return extJsAjaxResponse;
    }

    @ApiOperation(value = "更新Mongo日志文档")
    @PostMapping("/update")
    @ResponseBody
    public ExtJsAjaxResponse updateMongoLog(@RequestBody LogMongoSpt logMongoSpt) {
        Query query = new Query(Criteria.where("_id").is(logMongoSpt.getId()));
        Update update = new Update();
        update.set("input", logMongoSpt.getInput());
        update.set("output", logMongoSpt.getOutput());
        UpdateResult updateResult = mongoTemplate.upsert(query, update, LogMongoSpt.class);
        ExtJsAjaxResponse extJsAjaxResponse = null;
        if (updateResult.getModifiedCount() > 0) {
            extJsAjaxResponse = ExtJsAjaxResponse.createBySuccessMessage("更新Mongo文档成功");
        } else {
            extJsAjaxResponse = ExtJsAjaxResponse.createByErrorMessage("更新Mongo文档失败");
        }
        return extJsAjaxResponse;
    }

    @ApiOperation(value = "删除Mongo日志文档")
    @PostMapping("/delete")
    @ResponseBody
    public ExtJsAjaxResponse deleteMongoLog(@NotBlank(message = "id不能为空") @RequestParam(value = "id") String id) {
        Query query = new Query(Criteria.where("_id").is(id));
        DeleteResult deleteResult = mongoTemplate.remove(query, LogMongoSpt.class);
        ExtJsAjaxResponse extJsAjaxResponse = null;
        if (deleteResult.getDeletedCount() > 0) {
            extJsAjaxResponse = ExtJsAjaxResponse.createBySuccessMessage("删除Mongo日志数据成功");
        } else {
            extJsAjaxResponse = ExtJsAjaxResponse.createByErrorMessage("删除Mongo日志失败");
        }
        return extJsAjaxResponse;
    }

    @ApiOperation(value = "分页查询所有Mongo日志数据")
    @PostMapping("/queryAll")
    @ResponseBody
    public ExtJsAjaxResponse queryMongoLog(@NotBlank(message = "页码不能为空") @RequestParam(value = "page") int pageNo,
                                           @NotBlank(message = "每页数量不能为空") @RequestParam(value = "limit") int pageNum) {

        Query query = new Query();
        int totalCount = (int) mongoTemplate.count(query, LogMongoSpt.class);
        List<LogMongoSpt> logMongoSptList = mongoTemplate.find(query.skip(pageNo).limit(pageNum), LogMongoSpt.class);
        LogMongoPage logMongoPage = new LogMongoPage();
        logMongoPage.setRecords(logMongoSptList);
        logMongoPage.setTotal(totalCount);
        ExtJsAjaxResponse extJsAjaxResponse = ExtJsAjaxResponse.createBySuccess(logMongoPage);
        return extJsAjaxResponse;
    }


    @ApiOperation(value = "获取mongo日志数据")
    @GetMapping("/getMongoById")
    @ResponseBody
    public ExtJsAjaxResponse<LogMongoSpt> getMongoLogById(String mongoId) {
        //String mongoIdTest="68daf0c3179f46f999ea2b58a061cc73";
        LogMongoSpt logMongoSpt = mongoTemplate.findById(mongoId, LogMongoSpt.class);
        return  ExtJsAjaxResponse.createBySuccess(logMongoSpt);
    }


    /**
     * @param fxbs 分析标识
     * @param fxlx 分析类型
     * @return
     */
    @ApiOperation(value = "根据fxbs和fxlx获取mongo日志数据")
    @GetMapping("/getMongoLogByFxbs")
    @ResponseBody
    public ExtJsAjaxResponse getMongoLogByFxbs(@NotEmpty(message = "fxbs不能为空") String fxbs,
                                               @NotEmpty(message = "fxlx不能为空") String fxlx) {
        try {
            LogSpt logSpt = logSptService.getBaseMapper().selectOne(new LambdaQueryWrapper<LogSpt>().eq(LogSpt::getFxbs, fxbs).eq(LogSpt::getFxlx, fxlx));
            LogMongoSpt logMongoSpt = mongoTemplate.findById(logSpt.getMongoId(), LogMongoSpt.class);
            return ExtJsAjaxResponse.createBySuccess(logMongoSpt);
        } catch (Exception e) {
            return ExtJsAjaxResponse.createByErrorCodeMessage(String.valueOf(AlyCode.ERROR.getCode()), e.getMessage());
        }
    }
}
